<template>
	<view class="content">
		<view class="block video-block">
			<view class="video-item">
				<view class="video" v-for="(item) in list" :key="item.id">
					<video @play="play('video_'+item.id)" :id="'video_'+item.id" :src="item.vurl" :title="item.vname"
						class="video-icon"></video>
					<view class="name">{{item.vname}}</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getVideoTrainList
	} from '@/api/train.js';
	export default {
		data() {
			return {
				list: [],
				tab: [],
				tabActive: 0,
				keyword: '',
				page: 1,
				limit: 10,
				isMore: true,
				videoContext: null,
				videoId: '',
			}
		},
		watch: {
			tabActive() {
				this.resetPage();
			}
		},
		methods: {
			searchConfirm(e) {
				const value = e.detail.value;
				if (this.keyword != value) {
					this.keyword = value;
					this.$nextTick(() => {
						this.resetPage();
					})
				}
			},
			play(id) {
				if (this.videoContext && this.videoId != id) {
					this.videoContext && this.videoContext.stop();
				}
				this.videoContext = uni.createVideoContext(id, this);
				this.videoId = id;
			},
			getList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getVideoTrainList({
					page: this.page,
					limit: this.limit,
					type: this.tabActive,
					name: this.keyword
				}).then((res) => {
					uni.hideLoading()
					console.log(res)
						this.list = res.modelList;
						const total = res.count
						this.total = total;
						this.isMore = total > this.page * this.limit;
						this.videoId = null;
						this.videoContext = null;
					
				}).catch((res) => {
					uni.hideLoading();
					this.showToast('加载失败');
				})
			},
			resetPage() {
				this.total = 0;
				this.page = 1;
				this.getList()
			}
		},
		onLoad() {
			this.resetPage()
		},
		onReachBottom() {
			if (this.isMore) {
				this.page++;
				this.getList();
			}
		}
	}
</script>

<style lang="scss">
	$box-shadow: 0 0 10px 0 #efefef;

	page {
		background: #FAFAFA;
	}

	.content {
		width: 100%;
		padding: 30rpx;

		.block {
			width: 100%;
			margin-bottom: 20rpx;
			position: relative;

			.search {
				width: 100%;
				height: 70rpx;
				padding: 0 20rpx;
				font-size: 28rpx;
				background: #F3F3F3;
				border-radius: 50rpx;
			}

			.iconfont.icon-sousuo {
				position: absolute;
				transform: translate(0, -50%);
				right: 10rpx;
				top: 50%;
				font-size: 42rpx;
				font-weight: bold;
				color: #999;
			}
		}

		.block.classify-tab-block {
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;

			.tab-content {
				display: flex;
				flex-direction: row;
				justify-content: left;
				overflow-x: scroll;

				.tab-item {
					padding: 10rpx 15rpx;
					font-weight: bold;
					font-size: 30rpx;
					margin-right: 20rpx;
					flex: none;
				}

				.tab-item.active {
					background: url("https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/job_hunting/home/tab_selected_bg.png") no-repeat right bottom;
					background-size: 70%;
				}
			}

			.iconfont {
				font-size: 32rpx;
				font-weight: bold;
				color: #000;
			}
		}

		.block.video-block {
			width: 100%;
			margin-top: 20rpx;

			.video-item {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.video {
					width: 100%;
					margin-bottom: 30rpx;
					display: flex;
					flex-direction: column;

					.video-icon {
						width: 100%;
					}

					.name {
						font-size: 30rpx;
						background-color: #fff;
						padding: 10rpx;
					}
				}

			}

			.video-item:nth-child(2n) {
				margin-right: 0rpx;
			}
		}

	}
</style>
